import { NextResponse } from "next/server";
import { getUserById } from "@/app/utils/db";
import { getTokenFromRequest, verifyToken } from "@/app/utils/auth";

export const runtime = 'nodejs';

export async function GET(request) {
  const token = getTokenFromRequest(request);
  if (!token) {
    return NextResponse.json({ user: null }, { status: 200 });
  }
  const payload = verifyToken(token);
  if (!payload) {
    return NextResponse.json({ user: null }, { status: 200 });
  }
  const user = getUserById(payload.sub);
  return NextResponse.json({ user: user || null }, { status: 200 });
}


